﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using JinianNet.JNCms.Models;

namespace JinianNet.JNCms.Filters
{
    public class ManagerFilter : ErrorMessagesFilter
    {
        public Users _user;
        public Users User
        {
            get { return _user; }
        }

        public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }

            if (!AuthorizeCore(filterContext.HttpContext))
            {
                HandleUnauthorizedRequest(filterContext);
            }
            else
            {
                //filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                //HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;
                //cachePolicy.SetProxyMaxAge(new TimeSpan(0));
                //cachePolicy.AddValidationCallback(CacheValidateHandler, null /* data */);
            }

            base.OnAuthorization(filterContext);
        }

        protected bool AuthorizeCore(HttpContextBase context)
        {
            HttpCookie cookie = context.Request.Cookies["User"];
            if (cookie != null && !string.IsNullOrEmpty(cookie.Value))
            {
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(cookie.Value);
                if (authTicket != null)
                {
                    if (context.Session["User"] != null)
                    {
                        _user = (Users)context.Session["User"];
                    }
                    if (_user == null)
                    {
                        _user = Data.DataAccess.CreateUsers().GetItem(authTicket.UserData);
                    }
                    if (_user != null && _user.IsAdministrator)
                    {
                        //此处进行权限验证
                        return true;
                    }
                }
                return false;
            }
            return false;
        }

        protected void HandleUnauthorizedRequest(AuthorizationContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("filterContext");
            }
            else
            {
                string path = context.HttpContext.Request.Path;
                string strUrl = "/Manage/Login?url={0}";
                context.Result = new RedirectResult(string.Format(strUrl, HttpUtility.UrlEncode(path)), false);// new HttpUnauthorizedResult();

                //context.HttpContext.Response.Redirect(string.Format(strUrl, HttpUtility.UrlEncode(path)), true);
                
            }
            
        }

    }
}
